package com.report.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.operator.Equal;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.dto.request.ReportImageReq;
import com.report.dto.response.ReportImageRes;
import com.report.dto.response.WebReportImageRes;
import com.report.entity.ReportImage;
import com.report.entity.Search;
import com.report.mapper.ReportImageMapper;
import com.report.service.ISearchService;
import com.report.service.ReportImageService;
import com.zmh.framework.oss.OssConfigure;
import com.zmh.framework.util.Base64ToMultipartFile;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/1 13:54
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class ReportImageServiceImpl extends ServiceImpl<ReportImageMapper, ReportImage> implements ReportImageService {

    private final OssConfigure ossConfigure;

    @Override
    public void saveReportImage(ReportImageReq reportImageReq) {
        ReportImage reportImage = Convert.convert(ReportImage.class, reportImageReq);
        if(reportImageReq.getShowImage()){
            MultipartFile multipartFile = Base64ToMultipartFile.base64ToMultipart(reportImageReq.getBase64());
            String s = ossConfigure.uploadFile(multipartFile);
            reportImage.setImage(ossConfigure.getAccessUrl()+s);
        }
        this.saveOrUpdate(reportImage);

    }

    private final BeanSearcher beanSearcher;

    @Override
    public Page<ReportImageRes> lists(ReportImageReq reportImageReq) {
        Page<ReportImageRes> page=new Page<>(reportImageReq.getPageNum(),reportImageReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(reportImageReq.getPageNum() - 1, reportImageReq.getPageSize())
                .field(ReportImageRes::getName, reportImageReq.getName()).op(Contain.class)
                .field(ReportImageRes::getIndustryId, reportImageReq.getIndustryId()).op(Equal.class)
                .build();
        SearchResult<ReportImageRes> search = beanSearcher.search(ReportImageRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        return page;
    }

    @Override
    public Page<WebReportImageRes> imageLists(ReportImageReq reportImageReq) {
        Page<WebReportImageRes> page=new Page<>(reportImageReq.getPageNum(),reportImageReq.getPageSize());
        Map<String, Object> build = MapUtils.builder().page(reportImageReq.getPageNum() - 1, reportImageReq.getPageSize())
                .field(WebReportImageRes::getName, reportImageReq.getName()).op(Contain.class)
                .field(WebReportImageRes::getDeleted1,0).op(Equal.class)
                .field(WebReportImageRes::getIndustryId, reportImageReq.getIndustryId()).op(Equal.class)
                .build();
        SearchResult<WebReportImageRes> search = beanSearcher.search(WebReportImageRes.class, build);
        page.setTotal(search.getTotalCount().longValue());
        page.setRecords(search.getDataList());
        //保存搜索条件信息
        if(StrUtil.isNotEmpty(reportImageReq.getName())){
            Search search1=new Search();
            search1.setName(reportImageReq.getName());
            if(StpUtil.isLogin()){
                search1.setMemberId(StpUtil.getLoginIdAsInt());
            }
            searchService.saveSearch(search1);
        }

        return page;
    }
    private final ISearchService searchService;

}
